//
//  BYPicturesCollectionView.m
//  School+
//
//  Created by Sean Sebastian on 16/5/6.
//  Copyright © 2016年 Sean Sebastian. All rights reserved.
//

#import "BYPicturesCollectionView.h"

#import "Masonry.h"
#import "UIImageView+WebCache.h"

#define StatusPictureViewIdentifier @"StatusPictureViewIdentifier"


@interface BlogPicsCollectionViewCell : UICollectionViewCell

@property (nonatomic , weak) UIImageView *iconView;

@end

@implementation BlogPicsCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    [self prepareUI];
    
    return self;
}

- (void)prepareUI
{
    [self.contentView addSubview:self.iconView];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.contentView).insets(UIEdgeInsetsMake(5, 5, 5, 5));
    }];
    
}

- (UIImageView *)iconView
{
    if (_iconView == nil) {
        UIImageView *iconView = [[UIImageView alloc] init];
        _iconView = iconView;
        return iconView;
    }
    return _iconView;
}


@end

////////////////////////////////////////////////////////////


@interface BYPicturesCollectionView () <UICollectionViewDelegate , UICollectionViewDataSource>

@property (nonatomic , strong) UICollectionViewFlowLayout *layout;

@end

@implementation BYPicturesCollectionView

- (void)setBlog:(Blog *)blog
{
    _blog = blog;
    [self reloadData];
}

// 根据blog计算配图的尺寸
- (CGSize)calcViewSize
{
    CGSize itemSize = CGSizeMake(150, 150);
    
    self.layout.itemSize = itemSize;
    self.layout.minimumInteritemSpacing = 0 ;
    self.layout.minimumLineSpacing = 0 ;
    
    CGFloat margin = 10;
    
    NSInteger count = self.blog.picDicts.count;
    
    if (count == 0) {
        return CGSizeZero;
    }
    
    if (count == 1) {
        CGSize size = CGSizeMake(150, 120);
        self.layout.itemSize = size;
        return size;
    }else if (count == 2) {
        CGSize size = CGSizeMake(100, 100);
        self.layout.itemSize = size;
        return size;
    }
    
    CGFloat width = 2 * itemSize.width + margin;
    return CGSizeMake(width, width);
    
    
}

-(UICollectionViewFlowLayout *)layout
{
    if (_layout == nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        layout.itemSize = CGSizeMake(120, 120);
        layout.minimumLineSpacing = 0 ;
        layout.minimumInteritemSpacing = 0 ;
        self.layout = layout;
        return layout;
    }
    return _layout;
}

- (instancetype)init
{
    self = [super initWithFrame:CGRectZero collectionViewLayout:self.layout];
    
    self.dataSource = self;
    self.delegate = self;
    
    [self registerClass:[BlogPicsCollectionViewCell class] forCellWithReuseIdentifier:StatusPictureViewIdentifier];
    
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{

        return self.blog.picDicts.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    BlogPicsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:StatusPictureViewIdentifier forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor clearColor];
    
    cell.iconView.image = [self.blog.picDicts[indexPath.item] objectForKey:self.blog.picURLArray[indexPath.item]];// <url : uiimage>
    

    NSLog(@"%@" , self.blog.picDicts[indexPath.item]);
    
    
    return cell;
}
@end

